import logging

from coin.strategy.accounting.pnl_balance_printer.pnl_balance_printer import FuturesPnlBalancePrinter
from coin.strategy.accounting.pnl_balance_printer.util import calculate_unrealized_pnl
from coin.support.pta.logic.pta_stats_calculator import (get_futures_multiplier, is_inverse_product)


class BitmexPnlBalancePrinter(FuturesPnlBalancePrinter):
  def __init__(self, products, bookmap, reserve_map, logger=None):
    super().__init__(products,
                     bookmap,
                     reserve_map,
                     logger=(logger or logging.getLogger('BitmexPnlBalancePrinter')))

  def _get_pos(self, product):
    pos = self._og.get_position(product)
    avg_entry_price = self._og.get_average_entry_price(product)

    book = self._bookmap.get_book(product)
    if book is None or not book.has_ask() or not book.has_bid():
      return None, None

    pos_multiplier = get_futures_multiplier('Futures', 'Bitmex', product.symbol)
    inverse = is_inverse_product(product)

    unrealized_pnl = pos_multiplier * calculate_unrealized_pnl(
        product, pos, avg_entry_price, book.ask0().price, book.bid0().price, inverse=inverse)

    return pos, unrealized_pnl
